<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>咨客_撤单</title>
		<link href="<c:url value="/resources/css/style.css" />" rel="stylesheet" type="text/css">
		<link href="<c:url value="/resources/css/default/easyui.css" />" rel="stylesheet" type="text/css">
		<link href="<c:url value="/resources/css/icon.css" />" rel="stylesheet" type="text/css">
		<script type="text/javascript" src="<c:url value="/resources/js/zDialog/zDrag.js" />"></script>
		<script type="text/javascript" src="<c:url value="/resources/js/zDialog/zDialog.js" />"></script>
		<script type="text/javascript" src="<c:url value="/resources/js/util.js" />"></script>
		<script type="text/javascript">
			var master = ${IF_MASTER_YES};
			function onReturn(){
				location.href = "${pageContext.request.contextPath}/consultToken/tokenList";
			}
			
			function onCancelTable(id){
				var row = $("#" + id + " tr").length;
				
				for(var i = row - 1; i >= 1; i--){					
					$("#" + id).find("tr").eq(i).remove();
				}
			}
			
			function onQuery(){						
				if($.trim($("#itemNo").val()) == ''){
					$.messager.alert('警告', "请填写房间号、手牌号或帐单编号!");
					return false;
				}
				
				$.ajax({
					url:'queryBilling',
					data:{'itemNo':$.trim($("#itemNo").val()),
						  'isCancel':'cancel',
						  'itemType':$("input:[name='itemType']:radio:checked").val()},
					type:'post',
					dataType:'json',
					async:false,
					cache:false,
					success:function(data){
						if(data.result == 'success'){
							//帐单信息
							$("#cancelBtn").unbind( "click" );
							$("#cancelDIV").show();
							
							$("#errorFRAME").hide();
							$("#errorFRAME").html('');
							
							$("#billingIds").val('');

							//关联房间
							$("#roomDIV").show();
							onCancelTable("roomTable");
							if(data.roomSize > 0){
								$(data.roomList).each(function(){
									var rownum = $("#roomTable tr").length;
									var row = "<tr>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + rownum + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + this['roomTypeName'] + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + this['room_no'] + "</td>";
									row += "</tr>";
									
									$("#roomTable").append(row);
								});
							}else{
								$("#roomTable").append("<tr><td colspan='3' style='border: 1px solid #7F9DB9;'>无关联房间</td></tr>");
							}
							
							//关联手牌
							$("#tokenDIV").show();
							onCancelTable("tokenTable");
							if(data.tokenSize > 0){
								$(data.tokenList).each(function(){
									var rownum = $("#tokenTable tr").length;
									var row = "<tr>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + rownum + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + this['tokenTypeName'] + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + this['token_no'] + "</td>";
									row += "</tr>";
									
									$("#tokenTable").append(row);
								});
							}else{
								$("#tokenTable").append("<tr><td colspan='3' style='border: 1px solid #7F9DB9;'>无关联手牌</td></tr>");
							}
							
							//关联帐单
							$("#billingDIV").show();
							onCancelTable("billingTable");
							if(data.billingSize > 0){
								var billingIds = "";
								$(data.billingList).each(function(){
									var rownum = $("#billingTable tr").length;
									var row = "<tr>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + rownum + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>";
									if(this['if_master'] == master)
										row += "主帐单";
									else
										row += "关联帐单";
									row += "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + this['billing_no'] + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>";
									if(this['status'] == 'A'){
										row += '未结帐';
										$("#cancelBtn").click(function(){
											onAuth();
										});
									}else if(this['status'] == 'C')
										row += '取消';
									else
										row += '已结帐';
									row += "</td>";
									row += "</tr>";
									
									$("#billingTable").append(row);
									
									billingIds += this['id'] + ";";
								});
								$("#billingIds").val(billingIds);
							}else{
								$("#billingTalbe").append("<tr><td colspan='3' style='border: 1px solid #7F9DB9;'>无帐单</td></tr>");
							}
							
							//消费明细
							$("#consumeDIV").show();
							//服务类
							onCancelTable("serviceTable");
							if(data.serviceSize > 0){
								$(data.serviceList).each(function(){
									var rownum = $("#serviceTable tr").length;
									var row = "<tr>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + rownum + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + this['productName'] + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>￥" + numFormat(this['price']) + "</td>";									
									row += "<td style='border: 1px solid #7F9DB9;'>" + this['unit'] + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + (this['roomNo'] == null ? '&nbsp;':this['roomNo']) + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + (this['tokenNo'] == null ? '&nbsp;':this['tokenNo']) + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + this['statusName'] + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + (this['workerNo'] == null ? '&nbsp;':this['workerNo']) + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + (this['turnTypeName'] == null ? '&nbsp;':this['turnTypeName']) + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + (this['startTime'] == null ? '&nbsp;':this['startTime']) + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + (this['endTime'] == null ? '&nbsp;':this['endTime']) + "</td>";						
									row += "<td style='border: 1px solid #7F9DB9;'>" + (this['is_free'] == 'N'?'否':'是') + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + (this['is_subtract'] == 'N'?'否':'是') + "</td>";									
									row += "</tr>";
									$("#serviceTable").append(row);
								});
							}else{
								$("#serviceTable").append("<tr><td colspan='13' style='border: 1px solid #7F9DB9;'>无服务类消费</td></tr>");
							}
							
							//棋牌类
							onCancelTable("chessTable");
							if(data.chessSize > 0){
								$(data.chessList).each(function(){
									var rownum = $("#chessTable tr").length;
									var row = "<tr>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + rownum + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + this['productName'] + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>￥" + numFormat(this['price']) + "</td>";									
									row += "<td style='border: 1px solid #7F9DB9;'>" + this['unit'] + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + (this['roomNo'] == null ? '&nbsp;':this['roomNo']) + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + (this['tokenNo'] == null ? '&nbsp;':this['tokenNo']) + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + this['statusName'] + "</td>";						
									row += "<td style='border: 1px solid #7F9DB9;'>" + (this['startTime'] == null ? '&nbsp;':this['startTime']) + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + (this['endTime'] == null ? '&nbsp;':this['endTime']) + "</td>";						
									row += "<td style='border: 1px solid #7F9DB9;'>" + (this['is_free'] == 'N'?'否':'是') + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + (this['is_subtract'] == 'N'?'否':'是') + "</td>";
									row += "</tr>";
									$("#chessTable").append(row);
								});
							}else{
								$("#chessTable").append("<tr><td colspan='11' style='border: 1px solid #7F9DB9;'>无棋牌类消费</td></tr>");
							}
							
							//其它类
							onCancelTable("otherTable");
							if(data.otherSize > 0){
								$(data.otherList).each(function(){
									var rownum = $("#otherTable tr").length;
									var row = "<tr>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + rownum + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + this['productName'] + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>￥" + numFormat(this['price']) + "</td>";	
									row += "<td style='border: 1px solid #7F9DB9;'>" + this['qty'] + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + this['unit'] + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + (this['roomNo'] == null ? '&nbsp;':this['roomNo']) + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + (this['tokenNo'] == null ? '&nbsp;':this['tokenNo']) + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + this['statusName'] + "</td>";						
									row += "<td style='border: 1px solid #7F9DB9;'>" + (this['is_free'] == 'N'?'否':'是') + "</td>";
									row += "<td style='border: 1px solid #7F9DB9;'>" + (this['is_subtract'] == 'N'?'否':'是') + "</td>";
									row += "</tr>";
									$("#otherTable").append(row);
								});
							}else{
								$("#otherTable").append("<tr><td colspan='10' style='border: 1px solid #7F9DB9;'>无其它类消费</td></tr>");
							}
						}else{
							if(data.result == 'noData'){								
								$("#errorFRAME").show();
								$("#errorFRAME").html("<font color='red'>无数据</font>");
							}else
								$.messager.alert('操作结果', "系统繁忙!");
						}
					}
				});
			}
			
			function onReset(){
				$("#itemNo").val('');
				$("input[name='itemType'][value=1]").attr("checked",true);
			}
			
			function onAuth(){
				$("#operatePass").val('');
				$("#operateAccount").val('');
				var diag = new Dialog();
				diag.Width = 300;
				diag.Height = 110;
				diag.Drag=false;
				diag.Title = "授权操作";
				diag.InvokeElementId = "passwordDiv";
				diag.OKEvent = function(){
					if($id("operatePass").value == '' || $id("operateAccount").value == ''){
						Dialog.alert("请授权!");
						return false;
					}
					diag.close();
					onCancelOrder();
				};
				diag.show();
			}
			
			function onCancelOrder(){
				alert($("#billingIds").val());
				$.ajax({
					url:'cancelAll',
					data:{'billingIds':$("#billingIds").val(),
						  'operateAccount':$("#operateAccount").val(),
						  'operatePass':$.trim($("#operatePass").val())},
					type:'post',
					dataType:'text',
					async:false,
					cache:false,
					success:function(data){						
						$("#operatePass").val('');
						
						if(data == 'success'){						
							$.messager.alert('操作结果', "撤单成功!");
						}else
							$.messager.alert('操作结果', data);
						
					}
				});
			}
		</script>
	</head>
	
	<body>
		<c:import url="/WEB-INF/resources/common/consult_header.jsp" />
		
		<h1>撤单</h1>
		
		<div style="float:left; width:1000px;margin-right: 15px;">
			<div>				
				<div style="max-height:30px;margin-left: 30px; padding-bottom:10px; border-bottom: 1px solid #000">
					编号：
					<input class="easyui-validatebox" style="width:150px;margin-right: 60px;" type="text"  name="itemNo" id="itemNo" />
					类型：
					<input type="radio" name="itemType" value="${tokenType}"  checked/>手牌&nbsp;&nbsp;&nbsp;&nbsp;
					<input type="radio" name="itemType" value="${roomType}" />房间&nbsp;&nbsp;&nbsp;&nbsp;
					<input type="radio" name="itemType" value="${billingType}" />帐单
					
	    			<a href="javascript:void(0)" class="easyui-linkbutton" style="margin-left: 200px;" onclick="onQuery()">查询</a>
	    			&nbsp;&nbsp;
	    			<a href="javascript:void(0)" class="easyui-linkbutton" onclick="onReset()">清空</a>
	    			&nbsp;&nbsp;
	    			<a href="javascript:void(0)" class="easyui-linkbutton" onclick="onReturn()">返回</a>
				</div>
			</div>
			
			<div id="billingFRAME" style="margin-left: 30px; display: none;"></div>
			
			<div id="errorFRAME" style="margin-left: 30px; display: none;"></div>
			
			<div id="billingDIV" style="margin-left: 30px; display: none;">
				<h1>帐单</h1>
				<table id="billingTable" style="border-collapse: collapse;width:300px;text-align: center;">
					<tr>
						<td style="border: 1px solid #7F9DB9;font-weight: bold;">序号</td>
						<td style="border: 1px solid #7F9DB9;font-weight: bold;">帐单类型</td>
						<td style="border: 1px solid #7F9DB9;font-weight: bold;">帐单编号</td>
						<td style="border: 1px solid #7F9DB9;font-weight: bold;">帐单状态</td>
					</tr>
				</table>
			</div>
			
			<div id="roomDIV" style="margin-left: 30px; display: none;">
				<h1>关联房间</h1>
				<table id="roomTable" style="border-collapse: collapse;width:200px;text-align: center;">
					<tr>
						<td style="border: 1px solid #7F9DB9;font-weight: bold;">序号</td>
						<td style="border: 1px solid #7F9DB9;font-weight: bold;">房间类型</td>
						<td style="border: 1px solid #7F9DB9;font-weight: bold;">房间号</td>
					</tr>
				</table>
			</div>
			
			<div id="tokenDIV" style="margin-left: 30px;margin-top: 15px; display:none;">
				<h1>关联手牌</h1>
				<table id="tokenTable" style="border-collapse: collapse;width:200px;text-align: center;">
					<tr>
						<td style="border: 1px solid #7F9DB9;font-weight: bold;">序号</td>
						<td style="border: 1px solid #7F9DB9;font-weight: bold;">手牌类型</td>
						<td style="border: 1px solid #7F9DB9;font-weight: bold;">手牌号</td>
					</tr>
				</table>
			</div>
			
			<div id="consumeDIV" style="display: none;">			
			<h1>服务类</h1>
			<table id="serviceTable" style="border-collapse: collapse;width:1000px;text-align: center;">
				<tr>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">序号</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">商品名</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">单价</td>					
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">单位</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">房间号</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">手牌</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">状态</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">技师工号</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">起钟类型</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">起钟时间</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">结束时间</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">赠送</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">冲单</td>
				</tr>
			</table>
			<h1>棋牌类</h1>
			<table id="chessTable" style="border-collapse: collapse;width:1000px;text-align: center;">
				<tr>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">序号</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">商品名</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">单价</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">单位</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">房间号</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">手牌</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">状态</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">起钟时间</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">结束时间</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">赠送</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">冲单</td>
				</tr>
			</table>
			<h1>消费</h1>
			<table id="otherTable" style="border-collapse: collapse;width:1000px;text-align: center;">
				<tr>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">序号</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">商品名</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">单价</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">数量</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">单位</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">房间号</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">手牌</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">状态</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">赠送</td>
					<td style="border: 1px solid #7F9DB9;font-weight: bold;">冲单</td>
				</tr>
			</table>
			</div>
			
			<div id="cancelDIV" style="display:none;margin-left: 200px; margin-top: 25px;">
				<input type="button" name="cancelBtn" id="cancelBtn" value="撤单">
				&nbsp;&nbsp;
				<input type="button" name="returnBtn" id="returnBtn" value="返回" onclick="onReturn()">
			</div>
		</div>
		
	
		<!-- 密码授权DIV -->
		<div id="passwordDiv" style="display: none;">
      		<table width="100%" style="margin-top: 20px;" border="0" align="center" cellpadding="4" cellspacing="4" bordercolor="#666666">
        		<tr>
          			<td width="80" align="right">授权帐号：</td>
          			<td><input type="password" id="operateAccount" name="operateAccount"/></td>
          		</tr>
          		<tr>
          			<td width="80" align="right">授权密码：</td>
          			<td><input type="password" id="operatePass" name="operatePass"/></td>
        		</tr>
      		</table>
    	</div>
    	<input type="hidden" name="billingIds" id="billingIds" />
	</body>
</html>